var $ = require("../../utils/Ajax.js");
Page({
  data: {
    theme: null,
    manage: null,
    profiles: null,
    date: null,
    time: null,
    menpiao: 0,
    vip: ["所有人", "1", "2", "3"],
    vipindex: 0
  },

  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: "预告信息"
    }) 
    this.setData({
      date: new Date().getFullYear() + "-" + (new Date().getMonth() + 1) + "-" + new Date().getDate(),
      time: new Date().getHours() + ":" + new Date().getMinutes()
    })

    
  },

  theme: function(e){
    this.setData({
      theme: e.detail.value
    })
  },

  manage: function (e) {
    this.setData({
      manage: e.detail.value
    })
  },

  menpiao: function(e){
    this.setData({
      menpiao: e.detail.value
    })
  },

  bindDate: function (e) {
    var date1 = e.detail.value.replace('年', "-");
    var date2 = date1.replace('月', "-");
    var date3 = date2.replace('日', "");
    this.setData({
      date: date3
    })
  },

  bindTime: function (e) {
    this.setData({
      time: e.detail.value
    })
  },

  bindVip: function (e) { 
    this.setData({
      vipindex: e.detail.value
    })
  },

  profiles: function (e) {
    this.setData({
      profiles: e.detail.value
    })
  },

  TrailerBtn: function(){
    var theme = this.data.theme;
    var manage = this.data.manage;
    var startTime = this.data.date + ' ' + this.data.time;
    var ticket = this.data.menpiao;
    var minLevel = this.data.vip[this.data.vipindex];
    if (minLevel == '所有人'){
      minLevel = 0;
    }
    var profiles = this.data.profiles;
    if (theme == null || manage == null || profiles == null){
      wx.showToast({
        title: '必填信息不能为空',
        icon: 'none',
        duration: 3000,
        mask: true
      })
    }
    $.Ajax("api/addLiveVideoNext","POST",{
      theme: theme,
      manage: manage,
      startTime: startTime,
      ticket: ticket,
      minLevel: minLevel,
      profiles: profiles
    },function(res){
      wx.showToast({
        title: res.m,
        icon: 'none',
        duration: 1500,
        mask: true
      })
      if(res.s){
        var addLiveVideoNext = res.d.liveVideoNext;
        for (var i = 0; i < addLiveVideoNext.length; i++) {
          var startTime = addLiveVideoNext[i].startTime.split(" ");
          addLiveVideoNext[i].startTime = startTime[0]
        }
        wx.setStorageSync('addLiveVideoNext', res.d.liveVideoNext);
        setTimeout(function(){
          wx.navigateBack({
            delta: 1
          })
        },2000)
      }
    })
  }
})